/**   
* @Title: BindController.java 
* @Package com.leesche.zcsp.node.web.controller.wechat.bind.bind 
* @Description:() 
* @author dennies yang
* @date 2016年10月20日 下午3:04:35 
* @version V1.0   
*/
package com.leesche.zcsp.node.web.controller.wechat.bind;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.leesche.zcsp.node.pay.wecha.utils.SignUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.leesche.zcsp.node.pay.wecha.timer.WechatTimer;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.model.PubDevice;
import com.leesche.zcsp.node.web.service.PubDeviceService;
import com.leesche.zcsp.node.web.service.impl.data.read.PubDeviceDto;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubDeviceCondition;
import com.leesche.zcsp.node.web.service.privilege.PrivilegeService;

/**
 * @Title: BindController.java
 * @Package com.leesche.zcsp.node.web.controller.wechat.bind.bind
 * @Description:(绑定控制)
 * @author dennies yang
 * @date 2016年10月20日 下午3:04:35
 * @version V1.0
 */
@Controller
@RequestMapping(value = "/wechat/bind/bind")
public class BindController {
	private static final Logger logger = Logger.getLogger(BindController.class);

	@Autowired
	PrivilegeService privilegeService;
	
	@Autowired
	PubDeviceService pubDeviceService;

	private final static String MIANVIEW = "wechat/bind/bind/mainView";
	
	/**
	 * 
	 * @Title: listView @Description: TODO(打开绑定界面) @param @param
	 * model @param @return @return String @throws
	 */
	@RequestMapping(value = "showView", method = RequestMethod.GET)
	public String showView(HttpServletRequest request,Model model) {
		//增加JSTOken的签名  为了调用微信JS
		String url = request.getScheme() + "://" + request.getServerName() + request.getRequestURI();
		if( StringUtils.isNotBlank(request.getQueryString())){
			url = url + "?"+ request.getQueryString();
		}
		logger.info("jstoken +" +url);
		model.addAttribute("jsret", SignUtil.sign(WechatTimer.JS_TIKET, url));

		return MIANVIEW;
	}

	/**
	 * 
	* @Title: add 
	* @Description: TODO(绑定保存) 
	* @param @param imme 设备唯一码
	* @param @param shortCode 设备解码
	* @param @param payCode 设备支付码
	* @param @param session
	* @param @return   
	* @return Map<String,Object>   
	* @throws
	 */
	@ResponseBody
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public Map<String, Object> add(String imme,String shortCode,String payCode,HttpSession session) {
		if(StringUtils.isBlank(imme)||StringUtils.isBlank(shortCode)||StringUtils.isBlank(payCode)){
			return WebResult.failure("数据输入错误！").toMap();
		}
		
		// 获取设备信息
		PubDeviceDto pubDeviceDto =this.findPubDeviceDto(imme, null, null);
		if(pubDeviceDto==null){
			return WebResult.failure("设备未注册").toMap();
		}
		//设备已经绑定简码
		if(StringUtils.isNotBlank(pubDeviceDto.getDeviceshortcode())){
			return WebResult.failure("设备已绑定简码："+pubDeviceDto.getDeviceshortcode()).toMap();
		}
		//设备已经绑定支付码
		if(StringUtils.isNotBlank(pubDeviceDto.getMac())){
			return WebResult.failure("设备已绑定支付码:"+pubDeviceDto.getMac()).toMap();
		}

		if(pubDeviceDto!=null){
		// 判断设备是否已经绑定过了
			if(StringUtils.isNotEmpty(pubDeviceDto.getMac())){
				return WebResult.failure("设备已经绑定").toMap();
			}
		}
		
		
		
		// 判断支付码是否绑定
			if(this.findPubDeviceDto(null, shortCode, null)!=null){
				return WebResult.failure("设备简码已使用过").toMap();
			}
		// 判断解码是否绑定过
			if(this.findPubDeviceDto(null, null, payCode)!=null){
				return WebResult.failure("设备支付码已使用过").toMap();
			}


		try {
			//执行绑定操作

			PubDevice pubDevice=pubDeviceService.selectByPrimaryKey(pubDeviceDto.getDeviceid());
			pubDevice.setImme(imme);
			pubDevice.setMac(payCode);
			pubDevice.setDeviceshortcode(shortCode);
			pubDevice.setAgentid(privilegeService.getCurrentPrincipal()==null?1L:privilegeService.getCurrentPrincipal().getUserid());
			if(pubDeviceDto==null){
		/*		if(pubDeviceService.insert(pubDevice)>0){*/
					return WebResult.success().info("设备未注册!").toMap();
			/*	}*/
				
			}else {
				if(pubDeviceService.editPubDeviceByImme(pubDevice)>0){
					return WebResult.success().info("绑定成功").toMap();
				}
			}
						
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			return WebResult.failure(e).toMap();
		}
		return WebResult.failure("绑定失败").toMap();

	}
	
	/***
	 * 
	* @Title: findPubDeviceDto 
	* @Description: TODO(查询设备信息) 
	* @param @param imme
	* @param @param shortCode
	* @param @param payCode
	* @param @return   
	* @return condition   
	* @throws
	 */
	private  PubDeviceDto  findPubDeviceDto(String imme,String shortCode,String payCode){
		QueryPubDeviceCondition  condition =new QueryPubDeviceCondition();
		condition.setImme(imme);
		condition.setMac(payCode);
		condition.setDeviceshortcode(shortCode);
		return pubDeviceService.findByCondition(condition);
	}

}
